package com.xiaochangwei.web.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xiaochangwei.web.beans.User;
import com.xiaochangwei.web.beans.UserInfo;
import com.xiaochangwei.web.dao.UserDao;
import com.xiaochangwei.web.service.UserService;

/**
 * @since 2017年1月5日 上午9:54:32
 * @author 肖昌伟 317409898@qq.com
 * @description
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;

	@Override
	public User getUserByName(String username) {
		return userDao.getUserByName(username);
	}

	@Override
	public void updateLoginCount(User user) {
		userDao.updateLoginCount(user);
	}

	@Override
	public void insertUser(User user) {
		userDao.insertUser(user);
	}

	@Override
	public UserInfo getUserInfo(Long userId) {
		return userDao.getUserInfo(userId);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void insertUserInfo(UserInfo userInfo) {
		userDao.insertUserInfo(userInfo);
		for (Long hobbyId : userInfo.getHobbys()) {
			userDao.addUserHobby(userInfo.getId(), hobbyId);
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void updateUserInfo(UserInfo userInfo) {
		userDao.updateUserInfo(userInfo);
		userDao.deleteUserHobby(userInfo.getId());
		for (Long hobbyId : userInfo.getHobbys()) {
			userDao.addUserHobby(userInfo.getId(), hobbyId);
		}
	}

	@Override
	public void updateUserContact(UserInfo userInfo) {
		userDao.updateUserContact(userInfo);
	}

	@Override
	public UserInfo getUserContact(Long id) {
		return userDao.getUserContact(id);
	}

	@Override
	public void updatePm(Long userId, Integer pm) {
		userDao.updatePm(userId, pm);
	}

}
